package ee.maoinvest.frontend.controller.command.enterpriserelationswithelectricitymetersandcontractscommands;

import java.sql.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityContract;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.backend.services.ElectricityContractService;
import ee.maoinvest.backend.services.EnterpriseRelationsWithElectricityMetersAndContractsService;
import ee.maoinvest.backend.services.LatestDatesService;
import ee.maoinvest.backend.services.implementations.ElectricityContractServiceImpl;
import ee.maoinvest.backend.services.implementations.EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl;
import ee.maoinvest.backend.services.implementations.LatestDatesServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor;

public class FillElectricityContractRelationsWithElectricityMeterAndEnterprisesFormConverterWithInitialDataCommand implements Command
{
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int ID_PARSE_ERROR_IDENTIFICATOR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor = 
				new EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor();
		ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter = null;
		List<EnterpriseContractWithElectricityMeter> listOfAllContractRelationsWithElectricityMetersAndEnterprises = null;
		EnterpriseRelationsWithElectricityMetersAndContractsService enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = null;
		ElectricityContractService electricityContractServiceImpl = null;
		ElectricityContract electricityContractWhichRelationsAreBeingModified = null;
		int contractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit = 
				enterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor.getContractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit(request);
		LatestDatesService latestDatesServiceImpl = null;
		
		if (contractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit != ID_PARSE_ERROR_IDENTIFICATOR)
		{
			enterpriseRelationsWithElectricityMetersAndContractsServiceImpl = 
					new EnterpriseRelationsWithElectricityMetersAndContractsServiceImpl();
			electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter = 
					new ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter();
			electricityContractServiceImpl = new ElectricityContractServiceImpl();
			electricityContractWhichRelationsAreBeingModified = electricityContractServiceImpl.getElectricityContractById(
					contractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit);
			latestDatesServiceImpl = new LatestDatesServiceImpl();
			Date latestDateAfterWhatDataManipulationIsPossible = latestDatesServiceImpl.getLatestDateAfterWhatDataManipulationIsPossible();
			listOfAllContractRelationsWithElectricityMetersAndEnterprises = 
					enterpriseRelationsWithElectricityMetersAndContractsServiceImpl.getAllContractRelationsWithElectricityMetersAndEnterprises(
							contractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit);
			
			electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.fillWithInitialData(
					latestDateAfterWhatDataManipulationIsPossible, 
					electricityContractWhichRelationsAreBeingModified, listOfAllContractRelationsWithElectricityMetersAndEnterprises);
			request.setAttribute("electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter", 
					electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("electricityContractWhichRelationsAreBeingModified", 
					electricityContractWhichRelationsAreBeingModified);
			session.setAttribute("listOfAllContractRelationsWithElectricityMetersAndEnterprises", 
					listOfAllContractRelationsWithElectricityMetersAndEnterprises);
			session.setAttribute("latestDateAfterWhatDataManipulationIsPossible", latestDateAfterWhatDataManipulationIsPossible);
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		else
		{
			operationResult = OPERATION_UNSUCCESSFUL;
		}
		
		return operationResult;
	}
}
